package com.ai.aicontact.execmgn.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.codehaus.stax2.ri.typed.NumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ai.aicontact.busi.entity.ChannelDef;
import com.ai.aicontact.busi.entity.CriticalPointDef;
import com.ai.aicontact.busi.entity.ExecLimitCfg;
import com.ai.aicontact.busi.service.ChannelDefService;
import com.ai.aicontact.busi.service.CriticalPointService;
import com.ai.aicontact.busi.service.ExecLimitCfgService;
import com.ai.aicontact.common.constant.GlobalConstant;
import com.ai.aicontact.common.util.NumberUtils;
import com.ai.aicontact.common.vo.Grid;
import com.ai.aicontact.common.vo.Json;
import com.ai.aicontact.common.vo.PageFilter;
import com.ai.aicontact.common.vo.ZTree;
import com.alibaba.fastjson.JSON;

@Controller
@RequestMapping("/contactmgn/point")
public class DayLimitController {

	/*
	 * @RequestMapping("/{jspname}") public String web(@PathVariable("jspname")
	 * String jspname) { return "/contactmgn/point/" + jspname; }
	 */

	@Autowired
	private ChannelDefService channelDefService;
	@Autowired
	private CriticalPointService criticalPointService;
	@Autowired
	private ExecLimitCfgService execLimitCfgService;

	@Autowired
	private HttpServletRequest request;

	@RequestMapping("/daylimitcfglist")
	public String daylimitcfglist() throws Exception {
		HashMap<Long,String>  channelData = new HashMap<Long,String>();
		HashMap<Long,String> pointData = new HashMap<Long,String>();
		
		
		List<ChannelDef> channelDefList = channelDefService.getChannelDef();
		List<CriticalPointDef> criticalPointDefList = criticalPointService.getCriticalPointDefs();
	    
		for (ChannelDef channelDef : channelDefList) {
			channelData.put(channelDef.getChannelId(), channelDef.channelName); 
		}
		for (CriticalPointDef criticalPointDef : criticalPointDefList) {
			pointData.put(criticalPointDef.getCriticalPointId(), criticalPointDef.getCriticalPointName());
		}
		
		request.setAttribute("channelData", JSON.toJSONString(channelData));
		request.setAttribute("pointData", JSON.toJSONString(pointData));
		return "/contactmgn/point/daylimitcfglist";
	}

	@ResponseBody
	@RequestMapping("getPointTree")
	public Json getPointTree() throws Exception {

		Json json = new Json();

		try {
			List<ZTree> list = new ArrayList<ZTree>();
			List<ChannelDef> channelDefList = channelDefService.getChannelDef();
			List<CriticalPointDef> criticalPointDefList = criticalPointService.getCriticalPointDefs();

			if (channelDefList != null && channelDefList.size() > 0) {
				for (ChannelDef channelDef : channelDefList) {
					ZTree ztree = new ZTree();
					ztree.setId(channelDef.getChannelId());
					ztree.setName(channelDef.getChannelName()+"[渠]");
					ztree.setParent(true); 
					list.add(ztree);
				}
			}
			
			if (criticalPointDefList != null &&criticalPointDefList.size() > 0) {
				for (CriticalPointDef criticalPointDef : criticalPointDefList) {
					ZTree ztree = new ZTree();
					ztree.setId(criticalPointDef.getCriticalPointId());
					ztree.setName(criticalPointDef.getCriticalPointName()+"[触]");
					ztree.setParent(false);
					ztree.setpId(criticalPointDef.getChannelId());
					list.add(ztree);
				}
			}
			 
			json.setObj(list);
			json.setSuccess(true);
		} catch (Exception e) {
			json.setMsg(e.getMessage());
		}
		return json;
	}

	@ResponseBody
	@RequestMapping("getChannelTree")
	public Json getChannelTree() throws Exception {

		Json json = new Json();
		try {
			List<ZTree> listZTree = new ArrayList<ZTree>();
			List<ChannelDef> channelDefList = channelDefService.getChannelDef();

			if (channelDefList != null && channelDefList.size() > 0) {
				for (ChannelDef channelDef : channelDefList) {
					ZTree ztree1 = new ZTree();
					ztree1.setId(channelDef.getChannelId());
					ztree1.setName(channelDef.getChannelName());
					ztree1.setParent(false);
					listZTree.add(ztree1);
				}
			}
			json.setObj(listZTree);
			json.setSuccess(true);
		} catch (Exception e) {
			json.setMsg(e.getMessage());
		}
		return json;
	}

	@ResponseBody
	@RequestMapping("saveDayLimitCfg")
	public Json saveDayLimitCfg(ExecLimitCfg execLimitCfg) throws Exception {
		Json json = new Json();
		try {
			 
			execLimitCfgService.saveExecLimitCfgDayLimit(execLimitCfg);
			json.setObj(execLimitCfg);
			json.setSuccess(true);
			json.setMsg("保存成功");
		} catch (Exception e) {
			json.setMsg(e.getMessage());
		}
		return json;
	}

	@ResponseBody
	@RequestMapping("queryDaylimitCfg")
	public Grid queryDaylimitCfg(String keyWord, String channelId, String pointId, ExecLimitCfg execLimitCfg,
			PageFilter pf) throws Exception {
		Grid grid = new Grid();  
		List<ExecLimitCfg> rows = execLimitCfgService.queryExecLimitCfgDayLimit(keyWord, execLimitCfg, pf);
		Long total = execLimitCfgService.countExecLimitCfgDayLimit(keyWord, execLimitCfg);
		grid.setRows(rows);
		grid.setTotal(total);
		return grid;
	}

	@ResponseBody
	@RequestMapping("delDayLimitCfg")
	public Json delDayLimitCfg(ExecLimitCfg execLimitCfg) throws Exception {
		Json json = new Json();
		try {
			execLimitCfgService.deleteExecLimitCfgDayLimit(execLimitCfg);
			json.setSuccess(true);
			json.setMsg("删除成功");
		} catch (Exception e) {
			json.setMsg(e.getMessage());
		}
		return json;
	}

	@RequestMapping("/daylimitcfg")
	public String daylimitcfg(HttpServletRequest request, Long limitId) throws Exception {
		
		if (NumberUtils.isNotNullOrZero(limitId)) {
			ExecLimitCfg execLimitCfg = execLimitCfgService.queryExecLimitCfgDayLimitByLimitId(limitId);
			if (execLimitCfg != null && NumberUtils.isNotNullOrZero(execLimitCfg.getTargetId())) {
				Long targetId = execLimitCfg.getTargetId();

				if (execLimitCfg.getTargetType().equals(GlobalConstant.DAY_LIMIT_CFG_TARGET_TYPE_CHANNEL)) {
					ChannelDef channelDef = channelDefService.getChannelDefByChannelId(targetId); 
					execLimitCfg.setTargetName(channelDef.getChannelName());
				}
				if (execLimitCfg.getTargetType().equals(GlobalConstant.DAY_LIMIT_CFG_TARGET_TYPE_POINT)) {
					CriticalPointDef criticalPointDef = criticalPointService.getCriticalPointDefById(targetId);
					execLimitCfg.setTargetName(criticalPointDef.getCriticalPointName());
				}
				request.setAttribute("dayLimitCfg", execLimitCfg);
				request.setAttribute("targetTypeDisabled", "disabled");
				 
			}
			
		} 
		return "/contactmgn/point/daylimitcfg";
	}
}
